*     ***************************************************************** *;
*     ***************************************************************** *;
*       File-Name:      main.do                                     	*;
*       Date:           09/04/2020                                      *;
*       Authora:        Amodio, Baccini, Chiovelli, Di Maio             *;
*       Purpose:        Replication of "Agricultural Comparative        *;
*                       Advantage and Legislators' Support for Trade    *;
*                       Agreements,"                                    *;                                                                      
*       Input Files:    main_dataset.dta 		&				        *;
*						anes_dataset.dta 		        				*; 
*       Output Files:   JOP_logfile_main        &                       *;
*                       JOP_logfile_anes                                *:
*       Machine:        Office                                          *;
*       Program: 		Stata 15                                        *;
*     ****************************************************************  *;
*     ****************************************************************  *;
	
clear
clear matrix
clear mata
set maxvar 50000
set matsize 11000
capture 

cd "C:\Users\lbacci\Dropbox\Long_Term_Projects\Giorgio_Jeff_Results\Paper\Submission\Revisions\Analysis\Definitive"
use main_dataset, replace
log using JOP_logfile_main.log
set more off 

*Generate measure of agricoltural employment
drop occ_473_farming_pc ind_agri_pc
gen ind_agri_pc= ind_agri/pop
gen occ_473_farming_pc=occ_473_farming/pop

*Correct decade variable
replace decade=2010 if year>=2010

*Relabel main variables
rename trade_shock China_shock
label variable China_shock ""
rename ext1_x1y1 CA_export
rename ext2_x2y2 CA_import
label variable vote2 "Legislators' Voting in Favor of PTAs"
label variable agri_positive_rel_fta "PTAs Good for Agric."

*Take the log of the two main covariates
gen lnCA_export=ln(CA_export+0.0001)
label variable lnCA_export "Agri CA"
gen lnCA_import=ln(CA_import+0.0001)

** New CA measures Sean 
gen ln_measure_5_wld_crops_Excl_Brd =ln(measure_5_wld_crops_Excl_Brd)
gen ln_measure_5_wld_crops_Incl_Brd=ln(measure_5_wld_crops_Incl_Brd)
gen ln_measure_5_wld_agri_Excl_Brd=ln(measure_5_wld_agri_Excl_Brd)
gen ln_measure_5_wld_agri_Incl_Brd=ln(measure_5_wld_agri_Incl_Brd)
gen ln_measure_5_wld_manu_Incl_Brd=ln(measure_5_wld_manu_Incl_Brd) 
gen ln_measure_5_wld_manu_Excl_Brd=ln(measure_5_wld_manu_Excl_Brd)
gen lnservice_ratio_5yr=ln( service_ratio_5yr )
gen ln_measure_5_bi_manu_Incl_Brd=ln(measure_5_bi_manu_Incl_Brd)
gen ln_measure_5_bi_agri_Incl_Brd=ln(measure_5_bi_agri_Incl_Brd)
gen ln_measure_5_bi_crops_Incl_Brd=ln(measure_5_bi_crops_Incl_Brd) 

rename ln_measure_5_wld_manu_Incl_Brd p_tot_manu
rename ln_measure_5_wld_agri_Incl_Brd p_tot_agri
rename ln_measure_5_wld_crops_Incl_Brd p_tot_crops
rename lnservice_ratio_5yr p_tot_serv
label variable p_tot_manu "total export weighted by share of workers (manu)"
label variable p_tot_agri "total export weighted by share of workers (agri)"
label variable p_tot_crops "total export weighted by share of workers (crops)"
label variable p_tot_serv "total service production weighted by share of workers (service)"
gen p_ratio_tot_manu_exog=lnCA_export-p_tot_manu
gen p_ratio_tot_serv_exog=lnCA_export-p_tot_serv
gen p_ratio_tot_manu_exog1=p_tot_agri-p_tot_manu
gen p_ratio_tot_manu_exog2=p_tot_crops-p_tot_manu
gen p_ratio_tot_serv_exog1=p_tot_agri-p_tot_serv
gen p_ratio_tot_serv_exog2=p_tot_crops-p_tot_serv
gen p_ratio_tot_manu_endog=p_tot_agri-p_tot_manu
gen p_ratio_tot_serv_endog=p_tot_agri-p_tot_serv
gen p_ratio_tot_crops=p_tot_crops-p_tot_manu

gen ln_bal_bilat_5_in=ln(bal_bilat_5_in) if bal_bilat_5_in>0
gen ln_bal_bilat_5_in_neg=ln(-bal_bilat_5_in) if bal_bilat_5_in<0
replace ln_bal_bilat_5_in=-ln_bal_bilat_5_in_neg if bal_bilat_5_in<0
rename ln_bal_bilat_5_in p_bi_manu
gen p_ratio_bi_manu_exog=lnCA_export-p_bi_manu

label variable p_ratio_tot_manu_exog "Agri CA1"
label variable p_ratio_tot_manu_exog1 "Agri CA2"
label variable p_ratio_tot_serv_exog "Agri CAS1"
label variable p_ratio_tot_serv_exog1 "Agri CAS2"

* Adding Hiscox's controls
gen lncd_manu_profit_5yr=ln(cd_manu_profit_5yr)
gen cd_profit_manu_share=cd_manu_profit_5yr/state_manu_profit_5yr


/*
** Residual plots.

** basic correlation across measures of CA agri
binscatter p_ratio_tot_manu_exog lnCA_export, graphregion(color(white)) 
graph save Graph "C:\Users\lbacci\Dropbox\Long_Term_Projects\Giorgio_Jeff_Results\Paper\Submission\Revisions\Analysis\New\Graphs\AgriCA1_AgriCA2.gph", replace

binscatter p_ratio_tot_manu_exog1 lnCA_export, graphregion(color(white)) 
graph save Graph "C:\Users\lbacci\Dropbox\Long_Term_Projects\Giorgio_Jeff_Results\Paper\Submission\Revisions\Analysis\New\Graphs\AgriCA1_AgriCA3.gph", replace

gen leo=ln( ptnr_wld_ex_5yr_avg_mn_Incl_Brd )
gen leo1=ln( ptnr_wld_ex_5yr_avg_gr_Incl_Brd )
kdensity leo1, addplot(kdensity leo)
graph save Graph "C:\Users\lbacci\Dropbox\Long_Term_Projects\Giorgio_Jeff_Results\Paper\Submission\Revisions\Analysis\New\Graphs\prod_partner.gph"
gen leo2=ln( cd_wld_ex_dollars_5_mn_Incl_Brd )
gen leo3=ln( cd_wld_ex_dollars_5_gr_Incl_Brd )
kdensity leo3, addplot(kdensity leo2)
graph save Graph "C:\Users\lbacci\Dropbox\Long_Term_Projects\Giorgio_Jeff_Results\Paper\Submission\Revisions\Analysis\New\Graphs\prod_USdist.gph"
*/


########## Main Text Tables

*Table 1
*1
xi: reg vote2 lnCA_export, cluster( id_district ) r
outreg2 using table1.xls, bdec(3) tdec(2) keep(lnCA_export) label addtext(District FE, No, Decade FE, No, Main Controls, No, Additional Controls, No) 
margins, atmeans  at((p25) lnCA_export)  at((p75) lnCA_export)
gen e1=.6530335/.5324973
*2
xi: reg vote2 lnCA_export i.id_district, cluster( id_district ) r
outreg2 using table1.xls, bdec(3) tdec(2) keep(lnCA_export) label addtext(District FE, Yes, Decade FE, No, Main Controls, No, Additional Controls, No) append 
margins, atmeans  at((p25) lnCA_export)  at((p75) lnCA_export)
gen e2=.6269411/.565305
*3
xi: reg vote2 lnCA_export i.id_district i.decade, cluster( id_district ) r
outreg2 using table1.xls, bdec(3) tdec(2) keep(lnCA_export) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, No, Additional Controls, No) append
margins, atmeans  at((p25) lnCA_export)  at((p75) lnCA_export)
gen e3=.6256487/.5669301
*4
xi: reg vote2 lnCA_export offshorable democratic i.id_district i.decade, cluster( id_district ) r
outreg2 using table1.xls, bdec(3) tdec(2) keep(lnCA_export) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, No) append
margins, atmeans  at((p25) lnCA_export)  at((p75) lnCA_export)
gen e4=.6226434/.5668193
*5
xi: reg vote2 lnCA_export offshorable democratic p_bi_manu cd_manu_pct_5yr cd_profit_manu_share occ_473_farming_pc cd_pop_density i.id_district i.decade, cluster( id_district ) r
outreg2 using table1.xls, bdec(3) tdec(2) keep(lnCA_export) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, Yes) append
margins, atmeans  at((p25) lnCA_export)  at((p75) lnCA_export)
gen e5=.630415/.5750441
*6
xi: reg vote2 p_ratio_tot_manu_exog offshorable democratic i.id_district i.decade, cluster( id_district ) r
outreg2 using table1.xls, bdec(3) tdec(2) keep(p_ratio_tot_manu_exog) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, No) append
margins, atmeans  at((p25) p_ratio_tot_manu_exog)  at((p75) p_ratio_tot_manu_exog) 
gen e6=.6219924/.5690085 
*7
xi: reg vote2 p_ratio_tot_manu_exog1 offshorable democratic i.id_district i.decade, cluster( id_district ) r
outreg2 using table1.xls, bdec(3) tdec(2) keep(p_ratio_tot_manu_exog1) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, No) append
margins, atmeans  at((p25) p_ratio_tot_manu_exog1)  at((p75) p_ratio_tot_manu_exog) 
gen e7=.6090944/.5838491
drop e*

* Table 2 (Models 1-3)
*1
xi: reg agri_positive_rel_fta lnCA_export democratic offshorable if spoke_fta, cluster( id_district ) r 
outreg2 using table2.xls, bdec(4) tdec(2) keep(lnCA_export) label addtext(District FE, No, Wave FE, No, Main Controls, Yes, Individual Controls, No) 
margins, atmeans  at((p25) lnCA_export)  at((p75) lnCA_export) 
gen e1=.0017588 /.0009081
*2
xi: reg agri_positive_rel_fta p_ratio_tot_manu_exog democratic offshorable if spoke_fta, cluster( id_district ) r 
outreg2 using table2.xls, bdec(4) tdec(2) keep(p_ratio_tot_manu_exog) label addtext(District FE, No, Wave FE, No, Main Controls, Yes, Individual Controls, No) append
margins, atmeans  at((p25) p_ratio_tot_manu_exog)  at((p75) p_ratio_tot_manu_exog) 
gen e2=.0016551/.0010195
*3
xi: reg agri_positive_rel_fta p_ratio_tot_manu_exog1 democratic offshorable if spoke_fta, cluster( id_district ) r 
outreg2 using table2.xls, bdec(4) tdec(2) keep(p_ratio_tot_manu_exog1) label addtext(District FE, No, Wave FE, No, Main Controls, Yes, Individual Controls, No) append
margins, atmeans  at((p25) p_ratio_tot_manu_exog1)  at((p75) p_ratio_tot_manu_exog1) 
gen e3=.0017531/.000976


##### APPENDIX B

*Table B1
xi: reg vote2 lnCA_export i.id_district i.decade, cluster( id_district ) r
sum lnCA_export p_ratio_tot_manu_exog p_ratio_tot_manu_exog1 offshorable democratic cd_manu_pct_5yr cd_profit_manu_share occ_473_farming_pc cd_pop_density id_district decade if e(sample)


##### APPENDIX C

*Table C1
*1
xi: reg agri_positive_rel_fta lnCA_export if spoke_fta, cluster( id_district ) r 
outreg2 using tableC1.xls, bdec(4) tdec(2) e(rmse) keep(lnCA_export) label addtext(District FE, No, Wave FE, No, Main Controls, No, Individual Controls, No) 
*2
xi: reg agri_positive_rel_fta p_ratio_tot_manu_exog if spoke_fta, cluster( id_district ) r 
outreg2 using tableC1.xls, bdec(4) tdec(2) e(rmse) keep(p_ratio_tot_manu_exog) label addtext(District FE, No, Wave FE, No, Main Controls, No, Individual Controls, No) append
*3
xi: reg agri_positive_rel_fta p_ratio_tot_manu_exog1 if spoke_fta, cluster( id_district ) r 
outreg2 using tableC1.xls, bdec(4) tdec(2) e(rmse) keep(p_ratio_tot_manu_exog1) label addtext(District FE, No, Wave FE, No, Main Controls, No, Individual Controls, No) append
*4
xi: reg agri_positive_rel_fta lnCA_export democratic offshorable if spoke_fta, cluster( id_district ) r 
outreg2 using tableC1.xls, bdec(4) tdec(2) e(rmse) keep(lnCA_export) label addtext(District FE, No, Wave FE, No, Main Controls, Yes, Individual Controls, No) append
*5
xi: reg agri_positive_rel_fta p_ratio_tot_manu_exog democratic offshorable if spoke_fta, cluster( id_district ) r 
outreg2 using tableC1.xls, bdec(4) tdec(2) e(rmse) keep(p_ratio_tot_manu_exog) label addtext(District FE, No, Wave FE, No, Main Controls, Yes, Individual Controls, No) append
*6
xi: reg agri_positive_rel_fta p_ratio_tot_manu_exog1 democratic offshorable if spoke_fta, cluster( id_district ) r 
outreg2 using tableC1.xls, bdec(4) tdec(2) e(rmse) keep(p_ratio_tot_manu_exog1) label addtext(District FE, No, Wave FE, No, Main Controls, Yes, Individual Controls, No) append


##### APPENDIX D
label variable CA_export "Agri CA (no log)"

*Table D1
*1
xi: reg vote2 CA_export, cluster( id_district ) r
outreg2 using tableD1.xls, bdec(6) tdec(2) keep(CA_export) label addtext(District FE, No, Decade FE, No, Main Controls, No, Additional Controls, No) 
*2
xi: reg vote2 CA_export i.id_district, cluster( id_district ) r
outreg2 using tableD1.xls, bdec(6) tdec(2) keep(CA_export) label addtext(District FE, Yes, Decade FE, No, Main Controls, No, Additional Controls, No) append 
*3
xi: reg vote2 CA_export i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD1.xls, bdec(6) tdec(2) keep(CA_export) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, No, Additional Controls, No) append
*4
xi: reg vote2 CA_export offshorable democratic i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD1.xls, bdec(6) tdec(2) keep(CA_export) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, No) append
*5
xi: reg vote2 CA_export offshorable democratic p_bi_manu cd_manu_pct_5yr cd_profit_manu_share occ_473_farming_pc cd_pop_density i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD1.xls, bdec(6) tdec(2) keep(CA_export) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, Yes) append
*6
gen nolog_ratio1= CA_export/measure_5_wld_manu_Incl_Brd
gen nolog_ratio2= measure_5_wld_agri_Incl_Brd/measure_5_wld_manu_Incl_Brd
label variable nolog_ratio1 "Agri CA1 (no log)"
label variable nolog_ratio2 "Agri CA2 (no log)"
xi: reg vote2 nolog_ratio1 offshorable democratic i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD1.xls, bdec(7) tdec(2) keep(nolog_ratio1) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, No) append
*7
xi: reg vote2 nolog_ratio2 offshorable democratic i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD1.xls, bdec(6) tdec(2) keep(nolog_ratio2) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, No) append

*Table D2
rename w_ext1_x1y1 CA_export1
gen lnCA_export1=ln(CA_export1+0.0001)
label variable lnCA_export1 "Agri CA (Weighted on Prices)"

*1
xi: reg vote2 lnCA_export1, cluster( id_district ) r
outreg2 using tableD2.xls, bdec(2) tdec(2) keep(lnCA_export1 ) label addtext(District FE, No, Decade FE, No, Controls, No) 
*2
xi: reg vote2 lnCA_export1 i.id_district, cluster( id_district ) r
outreg2 using tableD2.xls, bdec(2) tdec(2) keep(lnCA_export1 ) label addtext(District FE, Yes, Decade FE, No, Controls, No) append 
*3
xi: reg vote2 lnCA_export1 i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD2.xls, bdec(2) tdec(2) keep(lnCA_export1 ) label addtext(District FE, Yes, Decade FE, Yes, Controls, No) append
*4
xi: reg vote2 lnCA_export1 offshorable democratic i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD2.xls, bdec(2) tdec(2) keep(lnCA_export1 ) label addtext(District FE, No, Decade FE, No, Controls, Yes) append
*5
xi: reg vote2 lnCA_export1 offshorable democratic i.id_district, cluster( id_district ) r
outreg2 using tableD2.xls, bdec(2) tdec(2) keep(lnCA_export1 ) label addtext(District FE, Yes, Decade FE, No, Controls, Yes) append
*6
xi: reg vote2 lnCA_export1 offshorable democratic i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD2.xls, bdec(2) tdec(2) keep(lnCA_export1 ) label addtext(District FE, Yes, Decade FE, Yes, Controls, Yes) append

*Table D3
rename ratio_1_sum_stdz CA_export2
gen lnCA_export2=ln(CA_export+0.0001)
label variable lnCA_export2 "Agri CA (Sum of Ratios)"

*1
xi: reg vote2 lnCA_export2, cluster( id_district ) r
outreg2 using tableD3.xls, bdec(2) tdec(2) keep(lnCA_export2 ) label addtext(District FE, No, Decade FE, No, Controls, No) 
*2
xi: reg vote2 lnCA_export2 i.id_district, cluster( id_district ) r
outreg2 using tableD3.xls, bdec(2) tdec(2) keep(lnCA_export2 ) label addtext(District FE, Yes, Decade FE, No, Controls, No) append 
*3
xi: reg vote2 lnCA_export2 i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD3.xls, bdec(2) tdec(2) keep(lnCA_export2 ) label addtext(District FE, Yes, Decade FE, Yes, Controls, No) append
*4
xi: reg vote2 lnCA_export2 offshorable democratic i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD3.xls, bdec(2) tdec(2) keep(lnCA_export2 ) label addtext(District FE, No, Decade FE, No, Controls, Yes) append
*5
xi: reg vote2 lnCA_export2 offshorable democratic i.id_district, cluster( id_district ) r
outreg2 using tableD3.xls, bdec(2) tdec(2) keep(lnCA_export2 ) label addtext(District FE, Yes, Decade FE, No, Controls, Yes) append
*6
xi: reg vote2 lnCA_export2 offshorable democratic i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD3.xls, bdec(2) tdec(2) keep(lnCA_export2 ) label addtext(District FE, Yes, Decade FE, Yes, Controls, Yes) append

*Table D4
*1
xi: reg vote2 p_ratio_tot_manu_exog, cluster( id_district ) r
outreg2 using tableD4.xls, bdec(3) tdec(2) keep(p_ratio_tot_manu_exog) label addtext(District FE, No, Decade FE, No, Main Controls, No, Additional Controls, No) 
*2
xi: reg vote2 p_ratio_tot_manu_exog i.id_district, cluster( id_district ) r
outreg2 using tableD4.xls, bdec(3) tdec(2) keep(p_ratio_tot_manu_exog) label addtext(District FE, Yes, Decade FE, No, Main Controls, No, Additional Controls, No) append 
*3
xi: reg vote2 p_ratio_tot_manu_exog i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD4.xls, bdec(3) tdec(2) keep(p_ratio_tot_manu_exog) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, No, Additional Controls, No) append
*4
xi: reg vote2 p_ratio_tot_manu_exog offshorable democratic i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD4.xls, bdec(3) tdec(2) keep(p_ratio_tot_manu_exog) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, No) append
*5
xi: reg vote2 p_ratio_tot_manu_exog offshorable democratic p_bi_manu cd_manu_pct_5yr cd_profit_manu_share occ_473_farming_pc cd_pop_density i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD4.xls, bdec(3) tdec(2) keep(p_ratio_tot_manu_exog) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, Yes) append

*Table D5
*1
xi: reg vote2 p_ratio_tot_manu_exog1, cluster( id_district ) r
outreg2 using tableD5.xls, bdec(3) tdec(2) keep(p_ratio_tot_manu_exog1) label addtext(District FE, No, Decade FE, No, Main Controls, No, Additional Controls, No) 
*2
xi: reg vote2 p_ratio_tot_manu_exog1 i.id_district, cluster( id_district ) r
outreg2 using tableD5.xls, bdec(3) tdec(2) keep(p_ratio_tot_manu_exog1) label addtext(District FE, Yes, Decade FE, No, Main Controls, No, Additional Controls, No) append 
*3
xi: reg vote2 p_ratio_tot_manu_exog1 i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD5.xls, bdec(3) tdec(2) keep(p_ratio_tot_manu_exog1) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, No, Additional Controls, No) append
*4
xi: reg vote2 p_ratio_tot_manu_exog1 offshorable democratic i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD5.xls, bdec(3) tdec(2) keep(p_ratio_tot_manu_exog1) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, No) append
*5
xi: reg vote2 p_ratio_tot_manu_exog1 offshorable democratic p_bi_manu cd_manu_pct_5yr cd_profit_manu_share occ_473_farming_pc cd_pop_density i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD5.xls, bdec(3) tdec(2) keep(p_ratio_tot_manu_exog1) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, Yes) append

*Table D6
*1
xi: reg vote2 p_ratio_tot_serv_exog, cluster( id_district ) r
outreg2 using tableD6.xls, bdec(3) tdec(2) keep(p_ratio_tot_serv_exog) label addtext(District FE, No, Decade FE, No, Main Controls, No, Additional Controls, No) 
*2
xi: reg vote2 p_ratio_tot_serv_exog i.id_district, cluster( id_district ) r
outreg2 using tableD6.xls, bdec(3) tdec(2) keep(p_ratio_tot_serv_exog) label addtext(District FE, Yes, Decade FE, No, Main Controls, No, Additional Controls, No) append 
*3
xi: reg vote2 p_ratio_tot_serv_exog i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD6.xls, bdec(3) tdec(2) keep(p_ratio_tot_serv_exog) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, No, Additional Controls, No) append
*4
xi: reg vote2 p_ratio_tot_serv_exog offshorable democratic i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD6.xls, bdec(3) tdec(2) keep(p_ratio_tot_serv_exog) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, No) append
*5
xi: reg vote2 p_ratio_tot_serv_exog offshorable democratic p_bi_manu cd_manu_pct_5yr cd_profit_manu_share occ_473_farming_pc cd_pop_density i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD6.xls, bdec(3) tdec(2) keep(p_ratio_tot_serv_exog) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, Yes) append

*Table D7
*1
xi: reg vote2 p_ratio_tot_serv_exog1, cluster( id_district ) r
outreg2 using tableD7.xls, bdec(3) tdec(2) keep(p_ratio_tot_serv_exog1) label addtext(District FE, No, Decade FE, No, Main Controls, No, Additional Controls, No) 
*2
xi: reg vote2 p_ratio_tot_serv_exog1 i.id_district, cluster( id_district ) r
outreg2 using tableD7.xls, bdec(3) tdec(2) keep(p_ratio_tot_serv_exog1) label addtext(District FE, Yes, Decade FE, No, Main Controls, No, Additional Controls, No) append 
*3
xi: reg vote2 p_ratio_tot_serv_exog1 i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD7.xls, bdec(3) tdec(2) keep(p_ratio_tot_serv_exog1) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, No, Additional Controls, No) append
*4
xi: reg vote2 p_ratio_tot_serv_exog1 offshorable democratic i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD7.xls, bdec(3) tdec(2) keep(p_ratio_tot_serv_exog1) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, No) append
*5
xi: reg vote2 p_ratio_tot_serv_exog1 offshorable democratic p_bi_manu cd_manu_pct_5yr cd_profit_manu_share occ_473_farming_pc cd_pop_density i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD7.xls, bdec(3) tdec(2) keep(p_ratio_tot_serv_exog1) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, Yes) append

*Table D8
*1
xi: reg vote2 lnCA_export China_shock i.decade, cluster( id_district ) r
outreg2 using tableD8.xls, bdec(5) tdec(3) keep(lnCA_export   China_shock) label addtext(District fixed effects, No, Decade fixed effects, Yes, Controls, No) 
*2
xi: ivreg2 vote2 lnCA_export (China_shock=z) i.decade, cluster( id_district ) r
outreg2 using tableD8.xls, bdec(5) tdec(3) keep(lnCA_export  China_shock) label addtext(District fixed effects, No, Decade fixed effects, Yes, Controls, No) append
*3
xi: reg vote2 lnCA_export offshorable democratic China_shock i.decade, cluster( id_district ) r
outreg2 using tableD8.xls, bdec(5) tdec(3) keep(lnCA_export  China_shock) label addtext(District fixed effects, No, Decade fixed effects, Yes, Controls, Yes) append
*4
xi: ivreg2 vote2 lnCA_export offshorable democratic (China_shock=z) i.decade, cluster( id_district ) r
outreg2 using tableD8.xls, bdec(5) tdec(3) keep(lnCA_export  China_shock) label addtext(District fixed effects, No, Decade fixed effects, Yes, Controls, Yes) append
*5
xi: reg vote2 lnCA_export offshorable democratic China_shock i.decade i.id_district, cluster( id_district ) r 
outreg2 using tableD8.xls, bdec(5) tdec(3) keep(lnCA_export  China_shock) label addtext(District fixed effects, Yes, Decade fixed effects, Yes, Controls, Yes) append
*6
xi: ivreg2 vote2 lnCA_export offshorable democratic (China_shock=z) i.decade i.id_district, cluster( id_district ) r partial(i.id_district)
outreg2 using tableD8.xls, bdec(5) tdec(3) keep(lnCA_export China_shock) label addtext(District fixed effects, Yes, Decade fixed effects, Yes, Controls, Yes) append
*7
xi: reg vote2 p_ratio_tot_manu_exog offshorable democratic China_shock i.decade i.id_district, cluster( id_district ) r 
outreg2 using tableD8.xls, bdec(5) tdec(3) keep(p_ratio_tot_manu_exog China_shock) label addtext(District fixed effects, Yes, Decade fixed effects, Yes, Controls, Yes) append
*8
xi: ivreg2 vote2 p_ratio_tot_manu_exog offshorable democratic (China_shock=z) i.decade i.id_district, cluster( id_district ) r partial(i.id_district)
outreg2 using tableD8.xls, bdec(5) tdec(3) keep(p_ratio_tot_manu_exog China_shock) label addtext(District fixed effects, Yes, Decade fixed effects, Yes, Controls, Yes) append
*9
xi: reg vote2 p_ratio_tot_manu_exog1 offshorable democratic China_shock i.decade i.id_district, cluster( id_district ) r 
outreg2 using tableD8.xls, bdec(5) tdec(3) keep(p_ratio_tot_manu_exog1 China_shock) label addtext(District fixed effects, Yes, Decade fixed effects, Yes, Controls, Yes) append
*10
xi: ivreg2 vote2 p_ratio_tot_manu_exog1 offshorable democratic (China_shock=z) i.decade i.id_district, cluster( id_district ) r partial(i.id_district)
outreg2 using tableD8.xls, bdec(5) tdec(3) keep(p_ratio_tot_manu_exog1 China_shock) label addtext(District fixed effects, Yes, Decade fixed effects, Yes, Controls, Yes) append

*Table D9
*1
xi: logit vote2 lnCA_export, cluster( id_district ) r
outreg2 using tableD9.xls, bdec(3) tdec(2) keep(lnCA_export) label addtext(District FE, No, Decade FE, No, Main Controls, No, Additional Controls, No) 
*2
xi: logit vote2 lnCA_export i.id_district, cluster( id_district ) r
outreg2 using tableD9.xls, bdec(3) tdec(2) keep(lnCA_export) label addtext(District FE, Yes, Decade FE, No, Main Controls, No, Additional Controls, No) append 
*3
xi: logit vote2 lnCA_export i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD9.xls, bdec(3) tdec(2) keep(lnCA_export) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, No, Additional Controls, No) append
*4
xi: logit vote2 lnCA_export offshorable democratic i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD9.xls, bdec(3) tdec(2) keep(lnCA_export) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, No) append
*5
xi: logit vote2 lnCA_export offshorable democratic p_bi_manu cd_manu_pct_5yr cd_profit_manu_share occ_473_farming_pc cd_pop_density i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD9.xls, bdec(3) tdec(2) keep(lnCA_export) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, Yes) append
*6
xi: logit vote2 p_ratio_tot_manu_exog offshorable democratic i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD9.xls, bdec(3) tdec(2) keep(p_ratio_tot_manu_exog) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, No) append
*7
xi: logit vote2 p_ratio_tot_manu_exog1 offshorable democratic i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD9.xls, bdec(3) tdec(2) keep(p_ratio_tot_manu_exog1) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, No) append

*Table D11
rename measure_5_wld_manu_Excl_Brd p_tot_manu_pla
rename measure_5_wld_agri_Excl_Brd p_tot_agri_pla
gen p_ratio_tot_manu_plac=ext3_x3y3/p_tot_manu_pla
gen p_ratio_tot_manu_plac1=p_tot_agri_pla/p_tot_manu_pla
label variable ext3_x3y3 "Agri CA (placebo)"
label variable p_ratio_tot_manu_plac "Agri CA1 (placebo)"
label variable p_ratio_tot_manu_plac1 "Agri CA2 (placebo)"

*1
xi: reg vote2 lnCA_export ext3_x3y3, cluster( id_district ) r
outreg2 using tableD11.xls, bdec(3) tdec(2) keep(lnCA_export ext3_x3y3) label addtext(District FE, No, Decade FE, No, Main Controls, No, Additional Controls, No) 
*2
xi: reg vote2 lnCA_export ext3_x3y3 i.id_district, cluster( id_district ) r
outreg2 using tableD11.xls, bdec(3) tdec(2) keep(lnCA_export ext3_x3y3) label addtext(District FE, Yes, Decade FE, No, Main Controls, No, Additional Controls, No) append 
*3
xi: reg vote2 lnCA_export ext3_x3y3 i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD11.xls, bdec(3) tdec(2) keep(lnCA_export ext3_x3y3) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, No, Additional Controls, No) append
*4
xi: reg vote2 lnCA_export ext3_x3y3 offshorable democratic i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD11.xls, bdec(3) tdec(2) keep(lnCA_export ext3_x3y3) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, No) append
*5
xi: reg vote2 lnCA_export ext3_x3y3 offshorable democratic p_bi_manu cd_manu_pct_5yr cd_profit_manu_share occ_473_farming_pc cd_pop_density i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD11.xls, bdec(3) tdec(2) keep(lnCA_export ext3_x3y3) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, Yes) append
*6
xi: reg vote2 p_ratio_tot_manu_exog p_ratio_tot_manu_plac offshorable democratic i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD11.xls, bdec(3) tdec(2) keep(p_ratio_tot_manu_exog p_ratio_tot_manu_plac) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, No) append
*7
xi: reg vote2 p_ratio_tot_manu_exog1 p_ratio_tot_manu_plac1 offshorable democratic i.id_district i.decade, cluster( id_district ) r
outreg2 using tableD11.xls, bdec(3) tdec(2) keep(p_ratio_tot_manu_exog1 p_ratio_tot_manu_plac1) label addtext(District FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, No) append

*Table D10
gen vote_sen=0 if Senate_Yea!=2
replace vote_sen=1 if Senate_Yea==2
sum vote_sen
replace vote_sen=. if Senate_Yea==.
sum vote_sen

preserve
collapse (mean) vote_sen offshorable democratic cd_manu_pct_5yr cd_profit_manu_share  occ_473_farming_pc p_bi_manu cd_pop_density decade (sum) lnCA_export p_ratio_tot_manu_exog p_ratio_tot_manu_exog1, by(id_state id_partner)
*1
xi: reg vote_sen lnCA_export, cluster( id_state ) r
outreg2 using tableD10.xls, bdec(3) tdec(2) keep(lnCA_export) label addtext(State FE, No, Decade FE, No, Main Controls, No, Additional Controls, No) 
*2
xi: reg vote_sen lnCA_export i.id_state, cluster( id_state ) r
outreg2 using tableD10.xls, bdec(3) tdec(2) keep(lnCA_export) label addtext(State FE, Yes, Decade FE, No, Main Controls, No, Additional Controls, No) append 
*3
xi: reg vote_sen lnCA_export i.id_state i.decade, cluster( id_state ) r
outreg2 using tableD10.xls, bdec(3) tdec(2) keep(lnCA_export) label addtext(State FE, Yes, Decade FE, Yes, Main Controls, No, Additional Controls, No) append
*4
xi: reg vote_sen lnCA_export offshorable democratic i.id_state i.decade, cluster( id_state ) r
outreg2 using tableD10.xls, bdec(3) tdec(2) keep(lnCA_export) label addtext(State FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, No) append
*5
xi: reg vote_sen lnCA_export offshorable democratic p_bi_manu cd_manu_pct_5yr cd_profit_manu_share occ_473_farming_pc cd_pop_density i.id_state i.decade, cluster( id_state ) r
outreg2 using tableD10.xls, bdec(3) tdec(2) keep(lnCA_export) label addtext(State FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, Yes) append
*6
xi: reg vote_sen p_ratio_tot_manu_exog offshorable democratic i.id_state i.decade, cluster( id_state ) r
outreg2 using tableD10.xls, bdec(3) tdec(2) keep(p_ratio_tot_manu_exog) label addtext(State FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, No) append
*7
xi: reg vote_sen p_ratio_tot_manu_exog1 offshorable democratic i.id_state i.decade, cluster( id_state ) r
outreg2 using tableD10.xls, bdec(3) tdec(2) keep(p_ratio_tot_manu_exog1) label addtext(State FE, Yes, Decade FE, Yes, Main Controls, Yes, Additional Controls, No) append
restore

log close
